package com.csyf.manager.service.impl;

import com.csyf.manager.dao.SysChannelAttrDao;
import com.csyf.manager.entity.SysChannelAttr;
import com.csyf.manager.service.SysChannelAttrBiz;
import com.csyf.manager.utils.page.JqGridVo;
import com.csyf.manager.utils.page.PageUtils;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @Author: casper
 * @Description:
 * @Date: Created in 2018/10/27 15:06
 */
@Slf4j
@Service
public class SysChannelAttrBizImpl implements SysChannelAttrBiz {

    @Autowired
    private SysChannelAttrDao sysChannelAttrDao;

    /**
     * 查询通道是否存在
     *
     * @param tradeType
     * @return
     */
    @Override
    public boolean isExistChannel(String tradeType) {
        SysChannelAttr vo = new SysChannelAttr();
        vo.setService(tradeType);
        int count = sysChannelAttrDao.selectCount(vo);
        return count > 0 ? true : false;
    }

    @Override
    public List<SysChannelAttr> findTradeType() {

        SysChannelAttr vo = new SysChannelAttr();
        vo.setFlag(0);
        List<SysChannelAttr> select = sysChannelAttrDao.select(vo);
        return select;
    }

    @Override
    public List<SysChannelAttr> findDfTradeType() {

        Example example = new Example(SysChannelAttr.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.orEqualTo("flag", 1).orEqualTo("flag", 2);

        List<SysChannelAttr> select = sysChannelAttrDao.selectByExample(example);
        return select;
    }


    @Override
    public SysChannelAttr findNoServiceByBalanceName(String balanceName) {
        SysChannelAttr vo = new SysChannelAttr();
        vo.setBalanceName(balanceName);
        vo.setFlag(1);
        return sysChannelAttrDao.selectOne(vo);
    }

    @Override
    public SysChannelAttr findDZServiceByBalanceName(String balanceName) {
        SysChannelAttr vo = new SysChannelAttr();
        vo.setBalanceName(balanceName);
        vo.setFlag(2);
        return sysChannelAttrDao.selectOne(vo);
    }

    @Override
    public SysChannelAttr findTradeByService(String serviceName) {
        SysChannelAttr vo = new SysChannelAttr();
        vo.setService(serviceName);
        return sysChannelAttrDao.selectOne(vo);
    }

    @Override
    public List<SysChannelAttr> findTradeByBalanceName(String balanceName) {
        SysChannelAttr vo = new SysChannelAttr();
        vo.setBalanceName(balanceName);
        vo.setFlag(0);
        return sysChannelAttrDao.select(vo);
    }

    /**
     * 通道配置列表
     * @param vo
     * @param page
     * @param rows
     * @return
     */
    @Override
    public JqGridVo queryChannelAttrList(SysChannelAttr vo, Integer page, Integer rows) {
        PageHelper.startPage(page, rows);

        Example example = new Example(SysChannelAttr.class);
        Example.Criteria criteria = example.createCriteria();
        PageUtils.andEqualTo(criteria, "service", vo.getService());

        List<SysChannelAttr> select = sysChannelAttrDao.selectByExample(example);
        return PageUtils.pasrJqGrid(select);
    }

    /**
     * 新增产品配置
     * @param channelAttrForm
     */
    @Override
    public void saveChannelAttr(SysChannelAttr channelAttrForm) {
        channelAttrForm.setChannelName("xxpay");
        channelAttrForm.setMinMoney(BigDecimal.ONE);
        channelAttrForm.setMaxMoney(new BigDecimal("99999"));
        channelAttrForm.setIsrate(0);
        channelAttrForm.setRate(BigDecimal.ONE);
        sysChannelAttrDao.insertSelective(channelAttrForm);
    }

    /**
     * 根据类型编辑产品配置
     * @param channelAttrForm
     * @return
     */
    @Override
    public int updateByService(SysChannelAttr channelAttrForm) {
        channelAttrForm.setChannelName("xxpay");
        channelAttrForm.setMinMoney(BigDecimal.ONE);
        channelAttrForm.setMaxMoney(new BigDecimal("99999"));
        channelAttrForm.setIsrate(0);
        channelAttrForm.setRate(BigDecimal.ONE);
        int tag = sysChannelAttrDao.updateByPrimaryKeySelective(channelAttrForm);
        return tag;
    }

    @Override
    public int deleteByService(String service) {
        int tag = sysChannelAttrDao.deleteByPrimaryKey(service);
        return tag;
    }


    @Override
    public List<String> queryAllBalanceName() {
        Set<String> set = new HashSet();
        List<SysChannelAttr> sysChannelAttrs = sysChannelAttrDao.selectAll();
        for (SysChannelAttr sysChannelAttr : sysChannelAttrs) {
            set.add(sysChannelAttr.getBalanceName());
        }
        return new ArrayList<>(set);
    }

    @Override
    public SysChannelAttr findBalanceNameByChanneNo(String balanceName) {
        SysChannelAttr vo = new SysChannelAttr();
        vo.setChannelName(balanceName);
        vo.setFlag(0);
        List<SysChannelAttr> select = sysChannelAttrDao.select(vo);
        if (!select.isEmpty()) {
            return select.get(0);
        }else{
            return null;
        }
    }
}
